package com.artislong;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.modelcontextprotocol.server.McpServer;
import io.modelcontextprotocol.server.McpServerFeatures;
import io.modelcontextprotocol.server.McpSyncServer;
import io.modelcontextprotocol.server.McpSyncServerExchange;
import io.modelcontextprotocol.server.transport.StdioServerTransportProvider;
import io.modelcontextprotocol.spec.McpSchema;
import io.modelcontextprotocol.spec.McpServerTransportProvider;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

/**
 * @author 陈敏
 * @version McpServerStdio.java, v 1.0 2025 07 24 16:33 chenmin Exp $
 * Created on 2025 07 24
 */
public class McpServerStdio {

    public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static void main(String[] args) {
        McpSchema.ServerCapabilities capabilities = McpSchema.ServerCapabilities.builder()
                .logging()
                .prompts(true)
                .resources(true, true)
                .tools(true)
                .completions()
                .experimental(new HashMap<>())
                .build();

        McpServerTransportProvider transportProvider = new StdioServerTransportProvider(OBJECT_MAPPER);

        // 创建并配置MCP服务器
        McpSyncServer mcpSyncServer = McpServer.sync(transportProvider)
                .serverInfo("my-server", "1.0.0")
                .capabilities(capabilities)
                .requestTimeout(Duration.ofSeconds(10))
                .build();

        mcpSyncServer.loggingNotification(McpSchema.LoggingMessageNotification.builder()
                .level(McpSchema.LoggingLevel.ERROR)
                .logger("mysql-mcp-logger")
                .data("Server initialized")
                .build());

        mcpSyncServer.addTool(toolSpecification());

//        mcpSyncServer.closeGracefully();
    }

    private static McpServerFeatures.SyncToolSpecification toolSpecification() {
        // 配置工具
        String jsonSchema = """
                {
                	"type":"object",
                	"properties":{
                		"userId" : {
                			"type": "string",
                			"description": "用户ID"
                		},
                		"orderId" : {
                			"type": "string",
                			"description": "订单号"
                		}
                	},
                	"required":[
                	    "userId",
                		"orderId"
                	],
                	"additionalProperties":false
                }
                """;
        // 方法的描述
        McpSchema.Tool tool = new McpSchema.Tool("getOrderDetail", "通过用户ID和订单号获取订单详情信息", jsonSchema);
        // 方法描述对应的执行逻辑
        BiFunction<McpSyncServerExchange, Map<String, Object>, McpSchema.CallToolResult> call = (exchange, arguments) -> {
            return new McpSchema.CallToolResult(JSONObject.toJSONString(new OrderDetail(MapUtil.getStr(arguments, "orderId"), "订单详情", MapUtil.getStr(arguments, "userId"))), false);
        };
        // 绑定描述和执行逻辑
        return new McpServerFeatures.SyncToolSpecification(tool, call);
    }

    public record OrderDetail(String orderId, String detail, String userId) {
    }

}
